package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.bo.AdminCategoryCreateBO;
import com.cskaoyan.bean.bo.AdminCategoryUpdateBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminCategoryL1VO;
import com.cskaoyan.bean.vo.AdminCategoryVO;
import com.cskaoyan.service.AdminCategoryService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 商场管理中的商品类名
 * \* User: 余雨健
 * \* Date: 2022/10/12
 * \* Time: 15:22
 **/
@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;

    /**
     *@商品类目的显示
     *@author yuyujian
     *@Date 2022/10/13
     *@Time 22:16
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData> list(){
        CommonData<AdminCategoryVO> data = adminCategoryService.selectCategoryList();
        return BaseRespVo.ok(data,"成功");
    }

    /**
     *@创建商品类目时父类类目名称的显示
     *@author yuyujian
     *@Date 2022/10/13
     *@Time 22:16
     */
    @RequestMapping("l1")
    public BaseRespVo<CommonData> l1(){
        CommonData<AdminCategoryL1VO> data = adminCategoryService.selectL1Category(0);
        return BaseRespVo.ok(data,"成功");
    }

    /**
     *@创建商品类目
     *@author yuyujian
     *@Date 2022/10/13
     *@Time 22:17
     */
    @PostMapping("create")
    public BaseRespVo<MarketStorage> create(@RequestBody AdminCategoryCreateBO adminCategoryCreateBO){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        MarketCategory marketCategory = new MarketCategory(null,adminCategoryCreateBO.getName(),adminCategoryCreateBO.getKeywords(),adminCategoryCreateBO.getDesc(),adminCategoryCreateBO.getPid(),adminCategoryCreateBO.getIconUrl(),adminCategoryCreateBO.getPicUrl(),adminCategoryCreateBO.getLevel(),null,date,date,null);
        int id = adminCategoryService.insertCategory(marketCategory);
        marketCategory.setId(id);
        return BaseRespVo.ok(marketCategory,"成功");
    }

    /**
     *@删除商品类目
     *@author yuyujian
     *@Date 2022/10/13
     *@Time 22:17
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody AdminCategoryVO adminCategoryVO){
        adminCategoryService.deleteCategory(adminCategoryVO.getId());
        return BaseRespVo.ok(null,"成功");
    }

    /**
     *@修改商品类目
     *@author yuyujian
     *@Date 2022/10/13
     *@Time 22:17
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketCategory marketCategory){
        adminCategoryService.updateCategory(marketCategory);
        return BaseRespVo.ok(null,"成功");
    }
}
